import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
// 登录验证
export default new Vuex.Store({
    state: {
        user: {},
        publishStatus: false, // 老师身份下 成绩发布状态
        gradeLocal: []
    },
    mutations: {
        // 登录
        login(state, user) {
            state.user = user;
        },
        // 退出
        logout(state, user) {
            state.user = {};
        },
        setPublishStatus(state, publishStatus) {
            state.publishStatus = publishStatus
        },
        setGradeLocal(state, params) {
            let deepParams = JSON.parse(JSON.stringify(params))
            console.log(deepParams);
            if (Object.prototype.toString.call(deepParams) == "[object Array]") {
                console.log('has clear gradeLocal');
                state.gradeLocal = []
            } else if (Object.prototype.toString.call(deepParams) == "[object Object]") {
                // let flag = state.gradeLocal.some(item => item.studentId == deepParams.studentId)
                // if (!flag) {
                //     state.gradeLocal.push(deepParams)
                // }
                let arr = state.gradeLocal.map(item => item.studentId)
                let flag = arr.indexOf(deepParams.studentId)
                if (flag > -1) {
                    state.gradeLocal.splice(flag, 1)
                }
                state.gradeLocal.push(deepParams)
            }
        }
    }
})